<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <link rel="stylesheet" href="/layui/css/formSelects-v4.css">
    <script src="/layui/layui.js"></script>
    <script src="/js/jquery.min.js"></script>
    <script src="/js/config.js"></script>
<body>
<h2>权限列表</h2>
<hr>
<div class="layui-row">
<table  class="layui-table" id="permissionTable" lay-filter="permissionTable"lay-toolbar='{{#toolbarDemo}}'>
</table>
</div>
<script>
    layui.config({
        base: '/layui/lay/modules/'
    }) .extend({
        formSelects: 'formSelects-v4'
    });
    var table ;
        layui.use(['table','tree','treeGrid' ,'formSelects','form'],function () {
            table=layui.table;
            var form=layui.form;
            var select = layui.formSelects;
        var treeGrid = layui.treeGrid;
        $.get(new Config().base_url + '/permission/all',function (data) {
            treeGrid.render({

                elem :"#permissionTable",
                data : data,

                treeId :'perid',
                treeUpId :'parentid',
                treeShowName :'pname',
                cols: [[
                      {type: 'checkbox'}
                    , {field: 'perid', title: '权限编号'}
                    , {field: 'pname', title: '权限名称'}
                    , {field: 'url', title: '连接地址'}
                    , {field: 'type',  title: '权限类型'},
                    {toolbar: '#barDemo' ,title:'操作'}
                ]],
                toolbar:'#toolbarDemo'
            });
    });
        form.render();
        treeGrid.on('tool(permissionTable)', function(obj){
            var data=obj.data;
            var layEvent=obj.event;
            switch(obj.event){
                case 'edit':
                    $.get("add.html",function (d) {
                        layer.open({
                            content:d,
                            type:1,
                            anim:1,
                            area:'800px',
                            btn:["确定","取消"],

                            success:function(){
                                data.locked = data.locked == 0 ? null : data.locked;
                                form.val('roleForm',data);
                                var u =setInterval(function () {
                                    console.log("waiting...");
                                    if ($("#perm_add option").length > 1){
                                        var ce=$("#perm_add");
                                        ce.val(data.parentid);
                                        layui.form.render();
                                        clearInterval(u);f
                                    }
                                })
                            },
                            yes:function () {
                                var str=$("#add_perm_form").serialize();
                                $.post(`${new Config().base_url}/permission/update`,str,function (data) {

                                    layer.close(layer.index);
                                    layer.msg("修改成功");
                                    table.reload('permissionTable');
                                });
                            }
                        })
                    });
                    break;
                case 'del':
                    if(layEvent === 'del'){

                        layer.confirm('真的删除么?',function (index) {
                            layer.close(index);
                            $.post(`${new Config().base_url}/permission/delete`,{perid:data.perid},function (d) {
                                if(d.code==0){
                                    $.get(new Config().base_url+'/permission/all',function (data) {
                                        treeGrid.render({
                                            elem :"#permissionTable",
                                            data : data,
                                            even:true,
                                            treeId :'perid',
                                            treeUpId :'parentid',
                                            treeShowName :'pname',
                                            cols: [[
                                                {type: 'checkbox'}
                                                , {field: 'perid', title: '权限编号'}
                                                , {field: 'pname', title: '权限名称'}
                                                , {field: 'url', title: '连接地址'}
                                                , {field: 'type',  title: '权限类型'},
                                                {toolbar: '#barDemo' ,title:'操作'}
                                            ]],
                                            toolbar:'#toolbarDemo'
                                        })
                                    });
                                }else{
                                    layer.msg("有内容不能删")
                                }

                            })
                        })

                    }
                    break;
            };
        });

    });

    function add() {
        $.post("add.html", function (d) {
            layer.open({
                type: 1,
                title: '添加应用',
                content: d,
                skin: 'layui-layer-molv',
                area: '800px',
                anim: 4,
                maxmin: true,
                btn: ["确定", "取消"],
                yes: function () {

                    var formdata = $("#add_perm_form").serialize();
                    $.post('http://localhost:8888/permission/add', formdata, function () {
                        layer.close(layer.index);
                        location.reload(true);
                    })

                }
            })
        })
    }
    $.get("http://localhost:8888/permission/all",function(data){
        $.each(data,function(){
            var opt = $("<option></option>").appendTo("#perm_add");
            opt.text(this.pname).val(this.perid);
        });
        layui.form.render();
    });



</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add" onclick="add()">添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDelete" onclick="batchDelete()">批量删除</button>

    </div>
</script>

<script type="text/html" id="barDemo">

    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>

</script>

</body>
</html>